﻿Public Class frmUsuarioListado

    Private Sub frmUsuarioListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgUsuarios.AutoGenerateColumns = False
        CargarUsuarios()
    End Sub

    Dim controller As New ControllerSigeco.controller_sistema()

    Private m_frmUsuario As frmUsuario
    Public Property frmUsuarios() As frmUsuario
        Get
            Return m_frmUsuario
        End Get
        Set(ByVal value As frmUsuario)
            m_frmUsuario = value
        End Set
    End Property

    Private Sub CargarUsuarios()

        Dim dt As New DataTable
        dt = controller.get_Usuario_All(oiml.dbConn, 0, "", txtNombre.Text, txtDocumento.Text, Not cbxHabilitado.Checked)
        dgUsuarios.DataSource = dt

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgUsuarios.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgUsuarios.SelectedRows(0).Cells(0).Value

                If frmUsuarios Is Nothing Then

                    frmUsuarios = New frmUsuario
                    frmUsuarios.codUsuario = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmUsuarios.ShowDialog()
                    CargarUsuarios()
                    frmUsuarios.Dispose()
                    frmUsuarios = Nothing
                    'End If
                Else
                    frmUsuarios.codUsuario = cod
                    frmUsuarios.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.Click
        CargarUsuarios()
    End Sub

    Private Sub txtProveedor_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNombre.KeyDown, txtDocumento.KeyDown, cbxHabilitado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarUsuarios()

        End If
    End Sub

    Private Sub dgUsuarios_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgUsuarios.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgUsuarios_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgUsuarios.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        frmUsuarios = New frmUsuario()
        frmUsuarios.codUsuario = 0
        frmUsuarios.ShowDialog()
        CargarUsuarios()
    End Sub
End Class